package com.iot.ic.controller;

import com.iot.ic.base.BaseController;
import com.iot.ic.common.Options;
import com.iot.ic.common.Response;
import com.iot.ic.common.ReturnCode;
import com.iot.ic.model.EquipmentInfo;
import com.iot.ic.service.EquipmentService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import java.util.Date;

/**
 * 设备相关接口
 */
@RestController
@Api(description = "设备相关接口")
public class EquipmentController extends BaseController{
	
	@Autowired
	private EquipmentService equipmentService;

	@ApiOperation(value = "增加设备信息", notes = "")
	@PostMapping(value = "/users/{userId}/equipment")
	public Response addEquipmentInfos(@PathVariable(value = "userId") Integer userId, @RequestBody @Valid EquipmentInfo equipmentInfo) {
	    Response response=new Response();
	    equipmentInfo.setDelFlag(Options.NotDel.getCode());
	    int result = equipmentService.addEquipmentInfo(equipmentInfo);
	    if (result==1)
	        response.setInfos(ReturnCode.No_AddEquipmentOK);
	    else
	        response.setInfos(ReturnCode.Err_AddEquipmentFailor);
	    return response;
	}
	
	
	@ApiOperation(value = "根据ID获得设备信息", notes = "")
	@RequestMapping(value="/users/{userId}/equipment/{equipmentId}", method=RequestMethod.GET)
	public Response getEquipmentInfoById(@PathVariable(value = "userId") Integer userId, 
			@PathVariable(value = "equipmentId") Integer equipmentId) {
	    Response response=new Response();
	    EquipmentInfo  equipmentInfo = equipmentService.getEquipmentInfoById(equipmentId);
	    response.setResult(equipmentInfo);
	    return response;
	}
	
	@ApiOperation(value = "根据ID更新设备信息", notes = "")
	@RequestMapping(value="/users/{userId}/equipment/{equipmentId}", method=RequestMethod.PUT)
	public Response updateEquipmentInfo(@PathVariable(value = "userId") Integer userId, 
			@PathVariable(value = "equipmentId") Integer equipmentId,
			@RequestBody @Valid EquipmentInfo equipmentInfo) {
	    Response response=new Response();
	    equipmentInfo.setEquipmentId(equipmentId);
	    int result = equipmentService.updateEquipmentInfo(equipmentInfo);
	    if (result==1)
	        response.setInfos(ReturnCode.No_UpdateEquipmentOK);
	    else
	        response.setInfos(ReturnCode.Err_UpdateEquipmentFailor);
	    return response;
	}
	
	@ApiOperation(value = "删除设备信息", notes = "")
	@RequestMapping(value="/users/{userId}/equipment/{equipmentId}", method=RequestMethod.DELETE)
	public Response deleteEquipmentInfo(@PathVariable(value = "userId") Integer userId, 
			@PathVariable(value = "equipmentId") Integer equipmentId) {
	    Response response=new Response();
	    int result = equipmentService.deleteEquipmentInfo(equipmentId);
	    if (result==1)
	        response.setInfos(ReturnCode.No_DeleteEquipmentOK);
	    else
	        response.setInfos(ReturnCode.Err_DeleteEquipmentFailor);
	    return response;
	}
}
